import { IPersonState } from "./State";
import { Component } from 'react';
import { IValidation } from "./Validation/IValidation";
import { PersonValidation } from "./Validation/PersonValidation";
import { AddressValidation } from "./Validation/AddressValidation";
import { PhoneNumberValidation } from "./Validation/PhoneNumberValidation";
import Col from "reactstrap/lib/Col";
import Row from "reactstrap/lib/Row";

interface IValidationProps {
  CurrentState: IPersonState;
  CanSave: (canSave: boolean) => void;
}

export default class FormValidation extends Component<IValidationProps> {
  private failures!: string[];
  private validation: IValidation;

  constructor (props: IValidationProps) {
    super(props);

    this.validation = new Array<IValidation>();
    this.validation.push(new PersonValidation());
    this.validation.push(new AddressValidation());
    this.validation.push(new PhoneNumberValidation());
  }

  public render () {
    this.Validate();
    const errors = this.failures.map(failure => {
      return (
        <Row key={failure}>
          <Col><label>{failure}</label></Col>
        </Row>
      )
    })

    return (<Col>{errors}</Col>)
  }

  private Validate () {
    this.failures = new Array<string>();
    this.validation.forEach(validation => {
      validation.Validate(this.props.CurrentState, this.failures);
    })

    this.props.CanSave(this.failures.length === 0);
  }
}